/*
 * eventQueue.c
 *
 *  Created on: Apr 12, 2015
 *      Author: mxw
 */
#include "eventQueue.h"
#include "commlib.h"

void init_event_queue(QueMgr* qmgr) {
    qmgr->cnt = 0;
    qmgr->read_pos = 0;
    qmgr->write_pos = 0;
    my_memset(qmgr->q, 0, sizeof(qmgr->q));
}

int push_event(QueMgr* qmgr, Queue_Element* pq_ele) {
    if (qmgr->write_pos >= MAX_Q_ITEM_CNT) {
        qmgr->write_pos = 0;
    }

    if (qmgr->cnt >= MAX_Q_ITEM_CNT) {
        return -1;
    }

    qmgr->q[qmgr->write_pos] = *pq_ele;
    ++qmgr->write_pos;
    ++qmgr->cnt;
    return 0;
}

Queue_Element* pick_msg(QueMgr* qmgr) {
    static Queue_Element ret;

    if (0 == qmgr->cnt) {
        return 0;
    }

    if (qmgr->read_pos >= MAX_Q_ITEM_CNT) {
        qmgr->read_pos = 0;
    }

    //ret = &qmgr->q[qmgr->read_pos];
    my_memcpy(&ret, &qmgr->q[qmgr->read_pos], sizeof(ret));
    
    --qmgr->cnt;
    ++qmgr->read_pos;
    return &ret;
}
